﻿using System;
using UnityEngine;
using UnityEngine.Rendering;

// Token: 0x0200014F RID: 335
[AddComponentMenu("RVP/C#/Effects/Tire Mark Creator", 0)]
[DisallowMultipleComponent]
[RequireComponent(typeof(Wheel))]
public class TireMarkCreate : MonoBehaviour
{
	// Token: 0x06000606 RID: 1542 RVA: 0x00030CEC File Offset: 0x0002EEEC
	private void Start()
	{
		this.tr = base.transform;
		this.w = base.GetComponent<Wheel>();
		this.initialEmissionRates = new float[this.debrisParticles.Length + 1];
		for (int i = 0; i < this.debrisParticles.Length; i++)
		{
			this.initialEmissionRates[i] = this.debrisParticles[i].emission.rateOverTime.constantMax;
		}
		if (this.sparks)
		{
			this.initialEmissionRates[this.debrisParticles.Length] = this.sparks.emission.rateOverTime.constantMax;
		}
	}

	// Token: 0x06000607 RID: 1543 RVA: 0x00030DA0 File Offset: 0x0002EFA0
	private void Update()
	{
		if (this.w.grounded)
		{
			this.alwaysScrape = ((!GroundSurfaceMaster.surfaceTypesStatic[this.w.contactPoint.surfaceType].alwaysScrape) ? 0f : (this.slipThreshold + Mathf.Min(0.5f, Mathf.Abs(this.w.rawRPM * 0.001f))));
		}
		else
		{
			this.alwaysScrape = 0f;
		}
		if (this.w.grounded && (Mathf.Abs(F.MaxAbs(new float[]
		{
			this.w.sidewaysSlip,
			this.w.forwardSlip
		})) > this.slipThreshold || this.alwaysScrape > 0f) && this.w.connected)
		{
			this.prevSurface = this.curSurface;
			this.curSurface = ((!this.w.grounded) ? -1 : this.w.contactPoint.surfaceType);
			this.poppedPrev = this.popped;
			this.popped = this.w.popped;
			if (!this.creatingMark)
			{
				this.prevSurface = this.curSurface;
				this.StartMark();
			}
			else if (this.curSurface != this.prevSurface || this.popped != this.poppedPrev)
			{
				this.EndMark();
			}
			if (this.curMark)
			{
				Vector3 a = Quaternion.AngleAxis(90f, this.w.contactPoint.normal) * this.tr.right * ((!this.w.popped) ? this.w.tireWidth : this.w.rimWidth);
				this.leftPoint = this.curMarkTr.InverseTransformPoint(this.w.contactPoint.point + a * this.w.suspensionParent.flippedSideFactor * Mathf.Sign(this.w.rawRPM) + this.w.contactPoint.normal * GlobalControl.tireMarkHeightStatic);
				this.rightPoint = this.curMarkTr.InverseTransformPoint(this.w.contactPoint.point - a * this.w.suspensionParent.flippedSideFactor * Mathf.Sign(this.w.rawRPM) + this.w.contactPoint.normal * GlobalControl.tireMarkHeightStatic);
			}
		}
		else if (this.creatingMark)
		{
			this.EndMark();
		}
		if (this.curEdge < GlobalControl.tireMarkLengthStatic && this.creatingMark)
		{
			this.UpdateMark();
		}
		else if (this.creatingMark)
		{
			this.EndMark();
		}
		for (int i = 0; i < this.debrisParticles.Length; i++)
		{
			if (this.w.connected)
			{
				if (i == this.w.contactPoint.surfaceType)
				{
					if (GroundSurfaceMaster.surfaceTypesStatic[this.w.contactPoint.surfaceType].leaveSparks && this.w.popped)
					{
						this.debrisParticles[i].emission.rateOverTime = this.zeroEmission;
						if (this.sparks)
						{
							this.sparks.emission.rateOverTime = new ParticleSystem.MinMaxCurve(this.initialEmissionRates[this.debrisParticles.Length] * Mathf.Clamp01(Mathf.Abs(F.MaxAbs(new float[]
							{
								this.w.sidewaysSlip,
								this.w.forwardSlip,
								this.alwaysScrape
							})) - this.slipThreshold));
						}
					}
					else
					{
						this.debrisParticles[i].emission.rateOverTime = new ParticleSystem.MinMaxCurve(this.initialEmissionRates[i] * Mathf.Clamp01(Mathf.Abs(F.MaxAbs(new float[]
						{
							this.w.sidewaysSlip,
							this.w.forwardSlip,
							this.alwaysScrape
						})) - this.slipThreshold));
						if (this.sparks)
						{
							this.sparks.emission.rateOverTime = this.zeroEmission;
						}
					}
				}
				else
				{
					this.debrisParticles[i].emission.rateOverTime = this.zeroEmission;
				}
			}
			else
			{
				this.debrisParticles[i].emission.rateOverTime = this.zeroEmission;
				if (this.sparks)
				{
					this.sparks.emission.rateOverTime = this.zeroEmission;
				}
			}
		}
	}

	// Token: 0x06000608 RID: 1544 RVA: 0x000312C0 File Offset: 0x0002F4C0
	private void StartMark()
	{
		this.creatingMark = true;
		this.curMark = new GameObject("Tire Mark");
		this.curMarkTr = this.curMark.transform;
		this.curMarkTr.parent = this.w.contactPoint.col.transform;
		this.curMark.AddComponent<TireMark>();
		MeshRenderer meshRenderer = this.curMark.AddComponent<MeshRenderer>();
		if (this.w.popped)
		{
			meshRenderer.material = this.rimMarkMaterials[Mathf.Min(this.w.contactPoint.surfaceType, this.rimMarkMaterials.Length - 1)];
		}
		else
		{
			meshRenderer.material = this.tireMarkMaterials[Mathf.Min(this.w.contactPoint.surfaceType, this.tireMarkMaterials.Length - 1)];
		}
		meshRenderer.shadowCastingMode = ShadowCastingMode.Off;
		this.mesh = this.curMark.AddComponent<MeshFilter>().mesh;
		this.verts = new Vector3[GlobalControl.tireMarkLengthStatic * 2];
		this.tris = new int[GlobalControl.tireMarkLengthStatic * 3];
		if (this.continueMark)
		{
			this.verts[0] = this.leftPointPrev;
			this.verts[1] = this.rightPointPrev;
			this.tris[0] = 0;
			this.tris[1] = 3;
			this.tris[2] = 1;
			this.tris[3] = 0;
			this.tris[4] = 2;
			this.tris[5] = 3;
		}
		this.uvs = new Vector2[this.verts.Length];
		this.uvs[0] = new Vector2(0f, 0f);
		this.uvs[1] = new Vector2(1f, 0f);
		this.uvs[2] = new Vector2(0f, 1f);
		this.uvs[3] = new Vector2(1f, 1f);
		this.colors = new Color[this.verts.Length];
		this.colors[0].a = 0f;
		this.colors[1].a = 0f;
		this.curEdge = 2;
		this.gapDelay = GlobalControl.tireMarkGapStatic;
	}

	// Token: 0x06000609 RID: 1545 RVA: 0x00031530 File Offset: 0x0002F730
	private void UpdateMark()
	{
		if (this.gapDelay == 0f)
		{
			float a = (float)((this.curEdge >= GlobalControl.tireMarkLengthStatic - 2 || this.curEdge <= 5) ? 0 : 1) * UnityEngine.Random.Range(Mathf.Clamp01(Mathf.Abs(F.MaxAbs(new float[]
			{
				this.w.sidewaysSlip,
				this.w.forwardSlip,
				this.alwaysScrape
			})) - this.slipThreshold) * 0.9f, Mathf.Clamp01(Mathf.Abs(F.MaxAbs(new float[]
			{
				this.w.sidewaysSlip,
				this.w.forwardSlip,
				this.alwaysScrape
			})) - this.slipThreshold));
			this.gapDelay = GlobalControl.tireMarkGapStatic;
			this.curEdge += 2;
			this.verts[this.curEdge] = this.leftPoint;
			this.verts[this.curEdge + 1] = this.rightPoint;
			for (int i = this.curEdge + 2; i < this.verts.Length; i++)
			{
				this.verts[i] = ((!Mathf.Approximately((float)i * 0.5f, Mathf.Round((float)i * 0.5f))) ? this.rightPoint : this.leftPoint);
				this.colors[i].a = 0f;
			}
			this.tris[this.curEdge * 3 - 3] = this.curEdge;
			this.tris[this.curEdge * 3 - 2] = this.curEdge + 3;
			this.tris[this.curEdge * 3 - 1] = this.curEdge + 1;
			this.tris[Mathf.Min(this.curEdge * 3, this.tris.Length - 1)] = this.curEdge;
			this.tris[Mathf.Min(this.curEdge * 3 + 1, this.tris.Length - 1)] = this.curEdge + 2;
			this.tris[Mathf.Min(this.curEdge * 3 + 2, this.tris.Length - 1)] = this.curEdge + 3;
			this.uvs[this.curEdge] = new Vector2(0f, (float)this.curEdge * 0.5f);
			this.uvs[this.curEdge + 1] = new Vector2(1f, (float)this.curEdge * 0.5f);
			this.colors[this.curEdge] = new Color(1f, 1f, 1f, a);
			this.colors[this.curEdge + 1] = this.colors[this.curEdge];
			this.mesh.vertices = this.verts;
			this.mesh.triangles = this.tris;
			this.mesh.uv = this.uvs;
			this.mesh.colors = this.colors;
			this.mesh.RecalculateBounds();
		}
		else
		{
			this.gapDelay = Mathf.Max(0f, this.gapDelay - Time.deltaTime);
			this.verts[this.curEdge] = this.leftPoint;
			this.verts[this.curEdge + 1] = this.rightPoint;
			for (int j = this.curEdge + 2; j < this.verts.Length; j++)
			{
				this.verts[j] = ((!Mathf.Approximately((float)j * 0.5f, Mathf.Round((float)j * 0.5f))) ? this.rightPoint : this.leftPoint);
				this.colors[j].a = 0f;
			}
			this.mesh.vertices = this.verts;
			this.mesh.RecalculateBounds();
		}
	}

	// Token: 0x0600060A RID: 1546 RVA: 0x0003197C File Offset: 0x0002FB7C
	private void EndMark()
	{
		this.creatingMark = false;
		this.leftPointPrev = this.verts[Mathf.RoundToInt((float)this.verts.Length * 0.5f)];
		this.rightPointPrev = this.verts[Mathf.RoundToInt((float)this.verts.Length * 0.5f + 1f)];
		this.continueMark = this.w.grounded;
		this.curMark.GetComponent<TireMark>().fadeTime = GlobalControl.tireFadeTimeStatic;
		this.curMark.GetComponent<TireMark>().mesh = this.mesh;
		this.curMark.GetComponent<TireMark>().colors = this.colors;
		this.curMark = null;
		this.curMarkTr = null;
		this.mesh = null;
	}

	// Token: 0x0600060B RID: 1547 RVA: 0x00031A51 File Offset: 0x0002FC51
	private void OnDestroy()
	{
		if (this.creatingMark && this.curMark)
		{
			this.EndMark();
		}
	}

	// Token: 0x04000940 RID: 2368
	private Transform tr;

	// Token: 0x04000941 RID: 2369
	private Wheel w;

	// Token: 0x04000942 RID: 2370
	private Mesh mesh;

	// Token: 0x04000943 RID: 2371
	private int[] tris;

	// Token: 0x04000944 RID: 2372
	private Vector3[] verts;

	// Token: 0x04000945 RID: 2373
	private Vector2[] uvs;

	// Token: 0x04000946 RID: 2374
	private Color[] colors;

	// Token: 0x04000947 RID: 2375
	private Vector3 leftPoint;

	// Token: 0x04000948 RID: 2376
	private Vector3 rightPoint;

	// Token: 0x04000949 RID: 2377
	private Vector3 leftPointPrev;

	// Token: 0x0400094A RID: 2378
	private Vector3 rightPointPrev;

	// Token: 0x0400094B RID: 2379
	private bool creatingMark;

	// Token: 0x0400094C RID: 2380
	private bool continueMark;

	// Token: 0x0400094D RID: 2381
	private GameObject curMark;

	// Token: 0x0400094E RID: 2382
	private Transform curMarkTr;

	// Token: 0x0400094F RID: 2383
	private int curEdge;

	// Token: 0x04000950 RID: 2384
	private float gapDelay;

	// Token: 0x04000951 RID: 2385
	private int curSurface = -1;

	// Token: 0x04000952 RID: 2386
	private int prevSurface = -1;

	// Token: 0x04000953 RID: 2387
	private bool popped;

	// Token: 0x04000954 RID: 2388
	private bool poppedPrev;

	// Token: 0x04000955 RID: 2389
	[Tooltip("How much the tire must slip before marks are created")]
	public float slipThreshold;

	// Token: 0x04000956 RID: 2390
	private float alwaysScrape;

	// Token: 0x04000957 RID: 2391
	[Tooltip("Materials in array correspond to indices in surface types in GroundSurfaceMaster")]
	public Material[] tireMarkMaterials;

	// Token: 0x04000958 RID: 2392
	[Tooltip("Materials in array correspond to indices in surface types in GroundSurfaceMaster")]
	public Material[] rimMarkMaterials;

	// Token: 0x04000959 RID: 2393
	[Tooltip("Particles in array correspond to indices in surface types in GroundSurfaceMaster")]
	public ParticleSystem[] debrisParticles;

	// Token: 0x0400095A RID: 2394
	public ParticleSystem sparks;

	// Token: 0x0400095B RID: 2395
	private float[] initialEmissionRates;

	// Token: 0x0400095C RID: 2396
	private ParticleSystem.MinMaxCurve zeroEmission = new ParticleSystem.MinMaxCurve(0f);
}
